package com.bocai.kelala.ui.travel.stroke.frag;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseFragment;
import com.bocai.kelala.bean.MyGradOrderBean;
import com.bocai.kelala.bean.MyPublishStrokeBean;
import com.bocai.kelala.bean.OrderDetailBean;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.stroke.act.MyReserveStrokeDetailActivity;
import com.bocai.kelala.ui.travel.stroke.ada.MyReserveStrokeAda;
import com.bocai.kelala.ui.travel.stroke.contract.MyStrokeContract;
import com.bocai.kelala.ui.travel.stroke.module.MyStrokeModule;
import com.bocai.kelala.ui.travel.stroke.presenter.MyStrokePresenter;
import com.bocai.mybase.base.BaseRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/6/13.
 */

public class MyReserveStrokeFrag extends BaseFragment implements MyStrokeContract.view,XRecyclerView.LoadingListener {
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    MyReserveStrokeAda reserveStrokeAda;
    @Inject
    MyStrokePresenter presenter;
    private int pageIndex = 1;


    @Override
    protected int getLayoutResource() {
        return R.layout.frag_my_reserve_stroke;
    }

    @Override
    public void initPresenter() {
        App.get(getContext()).getAppComponent().addSub(new MyStrokeModule(this)).inject(this);
    }

    @Override
    protected void initView() {
        reserveStrokeAda = new MyReserveStrokeAda();
        xrecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        xrecycle.setAdapter(reserveStrokeAda);
        reserveStrokeAda.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {

                Intent intent = new Intent(getActivity(),MyReserveStrokeDetailActivity.class);
                intent.putExtra("orderId",reserveStrokeAda.getDataList().get(index).getOrderId());
                if (reserveStrokeAda.getDataList().get(index).getOrderStatus() == 7){
                    intent.putExtra("flag",1);
                }
                if (reserveStrokeAda.getDataList().get(index).getOrderStatus() != 6) {
                    startActivityForResult(intent, 26);
                }
            }
        });
        xrecycle.setLoadingListener(this);
        xrecycle.refresh();

    }

    @Override
    public void myStrokePublish(MyPublishStrokeBean bean) {

    }

    @Override
    public void myGradOrder(MyGradOrderBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (bean != null &&bean.getOrderList()!=null&&bean.getOrderList().size()!=0){
            if (pageIndex == 1){
                reserveStrokeAda.clear();
                reserveStrokeAda.addItems(bean.getOrderList());
            }else{
                reserveStrokeAda.addItems(bean.getOrderList());
            }
            if (bean.getOrderList().size()<10){
                xrecycle.setLoadingMoreEnabled(false);
            }else{
                xrecycle.setLoadingMoreEnabled(true);
            }
        }else {
            xrecycle.setLoadingMoreEnabled(false);
        }
    }

    @Override
    public void getOrderDetail(OrderDetailBean bean) {

    }

    @Override
    public void orderPaySuccess(String result) {

    }

    @Override
    public void cancelOrderSuccess() {

    }

    @Override
    public void addDepartTimeSuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {

        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(getContext(),MainActivity.class).putExtra("index",1));
            getActivity().finish();
        }else if (msg.equals("网络异常")){
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
        showShortToast(msg);
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.myGrabOrder(pageIndex+"","10");
    }

    @Override
    public void onLoadMore() {
        ++pageIndex;
        presenter.myGrabOrder(pageIndex+"","10");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 26){
            pageIndex = 1;
            presenter.myGrabOrder(pageIndex+"","10");
        }
    }
}
